//
//  MusicViewController.m
//  FitTime
//
//  Created by Zone on 16/4/8.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "MusicViewController.h"
#import "MusicTableViewCell.h"
#import <AudioToolbox/AudioToolbox.h>

static SystemSoundID audio_music = 2;

@interface MusicViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger musicNumber;
@property (nonatomic, assign) NSInteger isSelect;

@end

@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.musicNumber = 100;
    self.isSelect = 100;
    
    self.view.backgroundColor = [UIColor whiteColor];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[MusicTableViewCell class] forCellReuseIdentifier:@"musicCell"];
    
    [self.view addSubview:self.tableView];
    
    UIButton *okButton = [UIButton buttonWithTitle:@"音乐选择.png" frame:CGRectZero target:self action:@selector(okAction)];
    
    [okButton.layer setCornerRadius:5];
    okButton.layer.masksToBounds = YES;
    [self.view addSubview:okButton];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.bottom.mas_equalTo(okButton.mas_top);
        make.right.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
    }];
    
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tableView.mas_bottom);
        make.bottom.mas_equalTo(self.view).offset(-2);
        make.right.mas_equalTo(self.view).offset(-2);
        make.left.mas_equalTo(self.view).offset(2);
        make.height.offset(70 * SCALEHEIGHT);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 23;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60 * SCALEHEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musicCell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if(indexPath.item == self.isSelect) {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"选中"]];
    } else {
        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlay"]];
    }
    cell.musicName = self.musicArr[indexPath.row];
    
    return cell;

}
#pragma mark 点击的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 点击播放铃声
    NSString *path = [[NSBundle mainBundle] pathForResource:self.musicArr[indexPath.row] ofType:@"mp3"];
    
    self.musicNumber = indexPath.row;
    
    AudioServicesDisposeSystemSoundID((SystemSoundID)audio_music);
    AudioServicesRemoveSystemSoundCompletion((SystemSoundID)audio_music);
    
    if (path) {
        //注册声音到系统
        NSURL *soundURL =[NSURL fileURLWithPath:path];
        OSStatus err = AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &audio_music);
        if(err!= kAudioServicesNoError){
            NSLog(@"Could not load %@,error code:%d",soundURL,err);
        }
    }
    
    AudioServicesPlaySystemSound((SystemSoundID)audio_music); //开始播放声音
    
    // 根据indexPath获取cell
    MusicTableViewCell *cell = (MusicTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    self.isSelect = indexPath.row;
    
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"选中"]];
    
}

#pragma mark 取消点击的方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MusicTableViewCell *cell = (MusicTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlay"]];
    
}


- (void)okAction {
    
    AudioServicesDisposeSystemSoundID((SystemSoundID)audio_music);
    AudioServicesRemoveSystemSoundCompletion((SystemSoundID)audio_music);
    [self.navigationController popViewControllerAnimated:YES];
    
    [self.delegate musicNumber:_musicNumber];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
